package cn.adelyn.base.auth.test;

import cn.adelyn.base.auth.AuthApplication;
import cn.adelyn.base.auth.controller.LoginController;
import cn.adelyn.base.auth.controller.TokenController;
import cn.adelyn.base.auth.feign.TokenFeignController;
import cn.adelyn.base.auth.pojo.dto.AuthenticationDTO;
import cn.adelyn.base.auth.pojo.dto.LogoutDTO;
import cn.adelyn.base.auth.pojo.dto.RefreshTokenDTO;
import cn.adelyn.base.auth.pojo.dto.RegisterAccountDTO;
import cn.adelyn.base.auth.service.AuthAccountService;
import cn.adelyn.base.auth.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;

@SpringBootTest(classes = AuthApplication.class)
@Slf4j
public class AuthTester {

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    AuthAccountService authAccountService;

    @Autowired
    LoginController loginController;

    @Autowired
    TokenController tokenController;

    @Autowired
    TokenFeignController tokenFeignController;

    @Autowired
    TokenService tokenService;

    @Test
    public void registerAccount() {
        RegisterAccountDTO registerAccountDTO = new RegisterAccountDTO();
        registerAccountDTO.setUserName("testAccount");
        registerAccountDTO.setPassword(passwordEncoder.encode("1234"));

        log.info(authAccountService.registerAccount(registerAccountDTO).toString());
    }

    @Test
    public void login() {
        AuthenticationDTO authenticationDTO = new AuthenticationDTO();
        authenticationDTO.setPrincipal("testAccount");
        authenticationDTO.setCredentials("1234");
        log.info(loginController.login(authenticationDTO).toString());
    }

    @Test
    public void logout() {
        LogoutDTO logoutDTO = new LogoutDTO();
        logoutDTO.setRefreshToken("0iekW3eOyqtAHDP7Dgzc3n/1UyoeZ6x3d+i5uaUdym5NFFvfO8VgB3ZUdqZO0NLB");
        log.info(loginController.loginOut(logoutDTO).toString());
    }

    @Test
    public void refreshToken() {
        RefreshTokenDTO refreshTokenDTO = new RefreshTokenDTO();
        refreshTokenDTO.setRefreshToken("11");
        log.info(tokenController.refreshToken(refreshTokenDTO).toString());
    }

    @Test
    public void checkToken() {
        log.info(tokenFeignController.checkToken("tVZFWd+aEkRuaBbdiWS6Pzpwjt1Y0q0Nc2oNRFiuU3hqfmprtjaBdgLb3zhqXxQ3").toString());
    }

    @Test
    public void delAllToken() {
        tokenService.deleteAllToken(1, 1619995941174120448L);
    }
}
